<template>
  <div class="m-container">
    <div class="plan-list-box">
      <div class="query-box">
        <el-select v-model="formGroup.gradeId" @change="getList" clearable placeholder="年级">
          <el-option v-for="o in queryMap.grades" :key="o.id" :label="o.name" :value="o.id" />
        </el-select>
        <el-select v-model="formGroup.semesterId" @change="getList" clearable placeholder="学期">
          <el-option v-for="o in queryMap.terms" :key="o.id" :label="o.name" :value="o.id" />
        </el-select>
      </div>
      <div class="content" v-loading="planLoading">
        <ul v-if="planList.length">
          <li v-for="item in planList" :key="item.id" :class="{ active: currentId === item.id }" @click="setCurrentId(item.id)">
            <span>{{ item.planName }}</span>
            <i class="el-icon-caret-right" />
          </li>
        </ul>
        <cus-empty v-else />
      </div>
    </div>
    <div class="class-times-box" v-loading="classTimesLoading">
      <ul v-if="classTimesList.length">
        <div class="all-check"><el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox></div>
        <li v-for="item in classTimesList" :key="item" @click="checkChange(item)">
          <el-checkbox :modelValue="checkedList.includes(item)" />
          <span>{{ item.indexName }}</span>
        </li>
      </ul>
      <cus-empty v-else />
    </div>
  </div>
</template>

<script lang="ts">
import { ref, reactive } from 'vue'
import $ from '$';
import { ElMessage } from 'element-plus';

export default {
  props: [ 'subjectId', 'excludId' ],
  setup(props) {
    let formGroup = reactive({
      subjectId: props.subjectId,
      excludId: props.excludId,
      gradeId: null,
      semesterId: null,
      current: 1,
      size: 99
    });

    let queryMap = reactive({ grades: [], terms: [] });

    let [ planLoading, setPlanLoading] = $.useState(false);
    let [ planList, setPlanList ] = $.useState([]);
    const getList = async () => {
      setPlanLoading(true);
      let res = await $.http.post('/admin/teachPlan/queryPage', { ...formGroup});
      setPlanList(res.json.records);
      setPlanLoading(false);
    }
    getList();

    let [ classTimesLoading, setClassTimesLoading ] = $.useState(false);
    let [ classTimesList, setClassTimesList ] = $.useState([]);
    const getClassTimesList = async (planId) => {
      setClassTimesLoading(true);
      let res = await $.http.post('/admin/teachTimes/query', { planId });
      setClassTimesLoading(false);
      setCheckList([]);
      setClassTimesList(res.json);
    }

    let [ currentId, setCurrentId ] = $.useState(null, getClassTimesList);

    let [ checkedList, setCheckList ] = $.useState<any[]>([]);
    
    
    let isIndeterminate = ref(true)
    let checkAll = ref(false)

    const handleCheckAllChange = (val) =>  {
      checkedList.value = val ? classTimesList.value : [];
      isIndeterminate.value = false;
    }
    const checkChange = (data) => {
      let index = checkedList.value.indexOf(data);
      index > -1 ? checkedList.value.splice(index, 1) : checkedList.value.push(data);
    }
    let userId = $.storage.get('userinfo').user.id;
    $.http.post('/permission/user/userDataRules', { userId, subjectCode: props.subjectId }).then(res => {
      queryMap.grades = res.json.grades;
      queryMap.terms = res.json.terms;
    });

    const save = (resolve, reject) => {
      if (checkedList.value.length) {
        resolve([checkedList.value, currentId.value])
      } else {
        reject(); ElMessage.warning('请选择教学计划！')
      }
    };

    return { formGroup, getList, queryMap, planLoading, planList, classTimesLoading, getClassTimesList, classTimesList, currentId, setCurrentId, checkedList, 
    checkChange, save, isIndeterminate, checkAll, handleCheckAllChange }
  }
}
</script>

<style lang="scss" scoped>
.m-container {
  display: flex;
  .plan-list-box {
    display: flex;
    flex-direction: column;
    width: 300px;
    .query-box {
      height: 36px + 16px * 2;
      padding: 16px 24px;
      :deep(.el-input) {
        width: 100px;
        margin-right: 10px;
      }
    }
    .content {
      flex: 1 1 78px;
      min-height: 300px;
      overflow: auto;
      li {
        display: flex;
        padding: 0 15px 0 24px;
        color: #333;
        line-height: 36px;
        border-bottom: solid 1px #DCDEE3;
        cursor: pointer;
        &.active {
          color: #72b22a;
          background: #F6F7F9;
          pointer-events: none;
          i {
            display: block;
          }
        }
        &:hover {
          background: #F6F7F9;
        }
        i {
          display: none;
          margin-left: auto;
          color: #77808D;
          line-height: 36px;
        }
      }
    }
  }
  .class-times-box {
    width: 190px;
    padding: 15px 20px;
    background: #F6F7F9;
    ul {
      overflow: auto;
    }
    li {
      cursor: pointer;
      overflow: hidden;
      white-space: nowrap;
      :deep(.el-checkbox) {
        margin-right: 8px;
        pointer-events: none;
      }
      &:not(:last-child) {
        margin-bottom: 18px;
      }
    }
  }
  .all-check {
    margin-bottom: 16px;
  }
}
</style>